package level
{
	import flash.filters.GlowFilter;
	import flash.geom.Point;
	import org.flixel.*;
	import utilities.BoxHighlight;
	import utilities.Countdown;
	
	import enemy.*;
	import treasure.*;
	import utilities.TextBox;
	
	/**
	 * Tutorial level
	 *
	 * @author Jesse Jeng
	 */
	public class Tutorial extends GameState
	{
		
		protected var currentEnemy:Enemy;
		protected var keys:FlxSprite;
		
		private static const BRUTE_SPEECH:uint = 0;
		private static const GHOST_SPEECH:uint = 1;
		private static const PRESS_DOWN:uint = 2;
		private static const PRESS_LEFT:uint = 3;
		private static const AWAIT_OWNAGE:uint = 4;
		private static const END:uint = 5;
		
		private var state:uint;
		private var listOfTextBox:Array;
		
		private var currentText:TextBox;
		
		public function Tutorial(currentLevelGrave:LevelGrave=null) { super(currentLevelGrave) };
		override public function create():void
		{
			super.create();
			
			_maze.load(StaticMaze.tutorial);
			
			//TODO: Save as constants
			_player.translateToTile(8, 5);
			
			addTreasure(new LootBag(8, 4));
			
			currentEnemy = addEnemy(new Robber(0, 14));
			
			_player.active = false;
			currentEnemy.active = false;
			
			listOfTextBox = new Array();
			
			listOfTextBox.push(new TextBox("INTRO_1", _player, "Hey! Is that a grave robber trying to steal my loot?"));
			listOfTextBox.push(new TextBox("INTRO_2", _player, "I better go stop him!"));
			
			listOfTextBox.reverse();
			
			keys = new FlxSprite(275, 50);
			keys.loadGraphic(Asset.ImgKeys, true, false, 463, 323);
			keys.addAnimation("PRESS_DOWN", [0, 1], 5);
			keys.addAnimation("PRESS_LEFT", [0, 2], 5);
			
			add(new Countdown().center());
					
			state = 0;
			
			add(keys);
			
			 add(new BoxHighlight(275, 50, 275+435, 50+323));
			
			//add(listOfTextBox[listOfTextBox.length - 1]);
		}
		
		override public function destroy():void
		{
			super.destroy();
		}
		
		override public function update():void
		{		
			super.update();
			
			if (state == BRUTE_SPEECH) {
				currentText = currentEnemy.say("I'm here to steal your loot!");
				state = GHOST_SPEECH
			}
			
			if (state == GHOST_SPEECH) {
				if(listOfTextBox.length != 0) {
					if (currentText.finished) {
						currentText.destroy();
						currentText = listOfTextBox.pop();
						add(currentText);
					}
				} else {
					if (currentText.finished) {
						keys.play("PRESS_DOWN");
						state = PRESS_DOWN
						_player.active = true;
					}
				}
			}
			
			if (state == PRESS_DOWN && FlxG.keys.justPressed("DOWN")) {
				keys.play("PRESS_LEFT");
				state = PRESS_LEFT;
			}
			
			if (state == PRESS_LEFT && FlxG.keys.justPressed("LEFT")) {
				state = AWAIT_OWNAGE;
				currentEnemy.active = true;
			}			
			
			if (state == AWAIT_OWNAGE && _enemies.countLiving() == 0) {
				currentText = new TextBox("", _player, "Alright I got him!");
				
				add(currentText);
				
				state = END;
			}
			if (state == END) {
				if (currentText.finished) {
					FlxG.fade(0xffaaaaaa, 1, onFade);
				}
			}
		}
		
		protected function onFade():void
		{
			FlxG.switchState(new ScoreState(_currentGrave));
		}
	}
}